var pageTemplate = '<div class="page"></div>';
var questionTemplate = '<li class="question"></li>';
var areAllSelected = false;
var numberQuestions, numberTries;


/**
 * initialization
 */
$().ready(function(){
	$('#tempo').bind('change', function(){
		if ($('#tempo').attr('checked')) {
			$('#displayTime').removeAttr('disabled');
		}else{
			$('#displayTime').attr('disabled', 'disabled');
		}
	});
	$('#tempo').trigger('change');
});

function initializeUserWordOptions() {
	$('#exerciceOptions #charSize').val(user.default_char_size);
	$('#exerciceOptions #displayTime').val(user.default_display_time);
	if(user.default_inverse == "1") {
		$('#exerciceOptions #inverse').prop('checked', 'checked');
	}
	if(user.default_tempo == "1" || selectedBatterie.default_tempo == "1") {
		$('#exerciceOptions #tempo').prop('checked', 'checked');
	}
	$('#tempo').trigger('change');
}

/**
 * Display Words
 */
function displayWords() {
	hideContainers();
	showContainer('wordChoiceContainer');
	initializeUserWordOptions();
	$(".actionLetsGo").focus();
}

/**
 * Going back from Words
 */
function backFromWords() {
	if (hasOnlyOneExercice && hasOnlyOneBatterie && hasOnlyOneSerie) {
		displayMainMenu();
	} else {
		if (hasOnlyOneSerie && hasOnlyOneBatterie) {
			displayBatteries();
		} else {
			if (hasOnlyOneSerie) {
				displayExercices();
			} else {
				displaySeries();
			}
		}
	}
}

/**
 * Empty series
 */
function emptyWords() {
	$('.menu.words').html('');
}

/**
 * Fill serie
 * @param idx
 * @param page
 */
function fillWords(idx, page) {
	var pageDom = $(pageTemplate);
	var currentPageLetter = pageI18NArray[idx];
	
	// fill page
	pageDom.addClass('page_' + currentPageLetter);
	
	// add title
	pageDom.append($('<a></a>').html(currentPageLetter).addClass('pageTitle').click(togleQuestionsOfPage.bind(this, currentPageLetter)));
	
	// fill questions
	var questionDom;
	$.each(page.question, function(idxQuestion, question) {
		questionDom = $(questionTemplate).addClass('q_page_' + currentPageLetter).addClass('q_idx_' + idxQuestion);
		questionDom.html($('<a/>').addClass('questionText').html(question.description).attr('href', '#').click(
			function(target){
				if (!!selectedWords[idx + '-' + idxQuestion]) {
					delete selectedWords[idx + '-' + idxQuestion];
					$(this).parent().removeClass('selected');
				}else{
					selectedWords[idx + '-' + idxQuestion] = {};
					selectedWords[idx + '-' + idxQuestion].page = idx;
					selectedWords[idx + '-' + idxQuestion].numero = idxQuestion;
					selectedWords[idx + '-' + idxQuestion].id = question.id;
					selectedWords[idx + '-' + idxQuestion].type_id = question.type_id;
					selectedWords[idx + '-' + idxQuestion].question = question.question;
					selectedWords[idx + '-' + idxQuestion].hasImage = question.has_image;
					selectedWords[idx + '-' + idxQuestion].image = question.image_name;
					selectedWords[idx + '-' + idxQuestion].hasSound = question.has_sound;
					selectedWords[idx + '-' + idxQuestion].sound = question.sound_name;
					selectedWords[idx + '-' + idxQuestion].description = question.description;
					selectedWords[idx + '-' + idxQuestion].reponse = question.reponse_complete;
					$(this).parent().addClass('selected');
				}
				return false;
			}
		)).click(function(event){$(this).find('a').trigger('click');});
		pageDom.append(questionDom);
		
		// increment the max of words per page if needed
		if (maxWordPerPage < idxQuestion) {
			maxWordPerPage++;
		}
	});
		
	$('.menu.words').append(pageDom);
}

function selectAllWords() {
	$.each($('li.question'), function(idxQuestion, question) {
		if (
			areAllSelected ? $(question).hasClass("selected") : !$(question).hasClass("selected")
		) {
			$(question).find('a').trigger('click');
		}
	});
	areAllSelected = !areAllSelected;
	
	return false;
}

function togleQuestionsOfPage(page) {
	$('li.question.q_page_' + page + ' a').trigger('click');
	return false;
}

function togleQuestionsOfIdx(idx) {
	$('li.question.q_idx_' + idx + ' a').trigger('click');
	return false;
}

/**
 * Display Training container
 */
function letsGo() {
	var word;
	var arrayIdx = 0;
	startTime = new Date();
	numberQuestions = 0;
	numberTries = 0;
	
	// create the array of selected words
	selectedWordsArray = new Array();
	for (wordIdx in selectedWords) {
		word = selectedWords[wordIdx];
		selectedWordsArray [arrayIdx++] = word;
		numberQuestions++;
	};
	
	if (selectedWordsArray.length == 0) {
		selectAllWords();
		letsGo();
		return;
	}
	
	// exercice type
	isDictee = $('#exerciceOptions #dictee').prop('checked');
	isInverse = $('#exerciceOptions #inverse').prop('checked');
	hasTempo = $('#exerciceOptions #tempo').prop('checked');
	tempo = calculateTempo($("#exerciceOptions #displayTime").val());
	
	if (isDictee) {
		$('.trainingContainer .wordTip').removeClass('hidden');
	}else{
		$('.trainingContainer .wordTip').addClass('hidden');
	}

	// display the first question
	hideContainers();
	displayNextQuestion();
	showContainer('trainingContainer');
	verticalAlign($('.trainingContainer'), (1/3));
	$('#answerInput').focus();
}
